<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIBlog
 *
 * 使用codeigniter框架开发的一个博客。
 * 
 * @package     ciblog
 * @author      Eric
 * @copyright   Copyright (c) 2011 - 2012, Eric
 * @license     GNU General Public License 2.0
 * @link        http://www.hebaijun.com
 * @version     1.0
 */

 //----------------------------------------------------------

 /**
  * 主页控制器
  *
  * 控制主页的一些显示信息
  *
  * @package     ciblog
  * @subpackage  Controllers
  * @category    Controllers
  * @author      Eric <baijun.he@163.com>
  * @link        http://www.hebaijun.com
  */
class Home extends MY_Controller {

	/**
	 * 默认方法
	 *
	 * @access   public
	 * @return   void
	 */
	public function index($current_page = '1')
	{
		//取得ciblog的所有设置项
		$data['bloginfo'] = $this->option_m->load_all_options();

		//当前位置
		$data['current_position'] = 'home';

		//标题
		$data['current_title'] = '';

		//单页导航
		$menu_pages = $this->page_m->load_pages('post_id,post_title');
		$data['menu_pages'] = $menu_pages;

		//分类目录
		$menu_cat = $this->category_m->load_all_category('term_id,term_name,term_count');
		$data['menu_cat'] = $menu_cat;

		//随机文章
		$limit_random = $data['bloginfo']['home_random_view_num'];
		$home_random_view_array = array(
			'limit' => $limit_random,
			'orderby' => 'post_id',
			'order' => 'random'
		);
		$home_random_view_result = $this->post_m->load_posts('post_id,post_title',$home_random_view_array);
		$data['home_random_view'] = $home_random_view_result;

		//分页
		$total_posts = $this->post_m->load_posts('COUNT(*) as total_rows');
		$limit = $data['bloginfo']['posts_per_page'];
		$config['base_url'] = base_url().'/pages/';
		$config['total_rows'] = $total_posts[0]->total_rows;
		$config['per_page'] = $limit; 
		$config['cur_page'] = $current_page; 
		$this->pages->initialize($config); 
		$data['pagination'] = $this->pages->create_links();

		//取文章
		$post_search_array = array(
			'limit' => $limit,
			'offset' => $limit*($current_page-1),
			'orderby' => 'post_date',
			'order' => 'DESC'
		);
		$posts = $this->post_m->load_posts('*',$post_search_array);
		foreach($posts as $key=>$post)
		{
			$posts[$key]->post_author = $this->user_m->get_user('id,username', array('id'=>$post->post_author));
			$posts[$key]->cat = $this->category_m->get_category('term_id,term_name',array('post_id'=>$post->post_id));
			$posts[$key]->tag = $this->tag_m->get_tag('term_id,term_name',array('post_id'=>$post->post_id));
		}
		$data['posts'] = $posts;

		$this->theme_view('index', $data);
	}

}

/* End of file home.php */
/* Location: ./application/controllers/home.php */